#include "directory.h"
//#include "debug.h"

char remdir[4096];

void remberdir(){
	getcwd ( remdir ,4096 );
}

void resumedir(){
	if (strlen(remdir))
		chdir( remdir );
	remdir[0]='\0';
}

void mchroot(){
	chdir( normaldir );
	return;
}

int mchdata(){
	strcpy( pathcatbuff , normaldir );
	strcat( pathcatbuff , DATAPATH );
	if(strcmp(nowdir,pathcatbuff)!=0){
			mchroot();
			chdir( pathcatbuff );
			return 1;
	}else{
			return 0;
	}
}

void mchdir(int l){
	if (!strlen(normaldir)){
		printf("InitDir  :  ");
		getcwd ( normaldir ,3584 );
		normaldir[1024]='\0';
		printf("%s\n",normaldir);
	}
	getcwd ( nowdir ,4096 );
	switch(l){
	case root:
		strcpy( pathcatbuff , normaldir );
		if (strcmp(nowdir,pathcatbuff)!=0)
				chdir( pathcatbuff );
		else
				return;
		break;
	case libs:
		strcpy( pathcatbuff , normaldir );
		strcat( pathcatbuff , LIBPATH );
		if(strcmp(nowdir,pathcatbuff)!=0){
				mchroot();
				chdir( pathcatbuff );
		}else{
				return;
		}
		break;
	case lua:
		strcpy( pathcatbuff , normaldir );
		strcat( pathcatbuff , LUAPATH );
		if(strcmp(nowdir,pathcatbuff)!=0){
				mchroot();
				chdir( pathcatbuff );
		}else{
				return;
		}
		break;
		
	case data:
		if (mchdata()==0) return;  
		break;

	default:
		break;
	}
	/*getcwd ( nowdir , 4096 );
	printf("Current Dir is : %s\n" , nowdir);*/
	return;
}
